package com.qf.flash.base;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.qf.flash.R;
import com.qf.flashlib.widget.dialog.BaseDialog;
import com.qf.flashlib.widget.dialog.ViewHolder;


/**
 * 2020/3/10 14:40
 */
public class CommonBottomChosenDialog extends BaseDialog {
    ListView mRecyclerView;
    private onItemClickListener onItemClickListener;
    private ArrayAdapter<String> arrayAdapter;
    private String[] data;


    public static CommonBottomChosenDialog newInstance() {
        CommonBottomChosenDialog dialog = new CommonBottomChosenDialog();
        dialog.setAnimStyle(R.style.public_dialog_inout_anim);
        dialog.setShowBottom(true);
        dialog.setOutCancel(true);
        return dialog;
    }
    @Override
    public int setUpLayoutId() {
        return R.layout.dialog_bottom_in;
    }

    @Override
    public void convertView(ViewHolder holder, BaseDialog dialog) {
        arrayAdapter = new ArrayAdapter<>(dialog.getContext(), R.layout.item_chosen_dialog);
        mRecyclerView = holder.getView(R.id.mRecyclerView);
        mRecyclerView.setAdapter(arrayAdapter);
        mRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(view, position);
                }
                dismiss();
            }
        });

        holder.getView(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        arrayAdapter.clear();
        arrayAdapter.addAll(data);
    }

    public CommonBottomChosenDialog setData(String[] data) {
        this.data = data;
        return this;
    }


    public CommonBottomChosenDialog setOnItemClickListener(CommonBottomChosenDialog.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    public interface onItemClickListener{
        void onItemClick(View view, int position);
    }

}
